package com.example.sh_crosses.db;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.sh_crosses.base.App;

import java.util.ArrayList;
import java.util.List;

public class RecordDao {
    private final String TAG = "RecordDa";
    public static final String TABLE_NAME = "record";
    private static final String ID = "id";
    private static final String COUNT = "count";
    private DBHelper dbHelper;
    //创建表结构
    public static final String SQL_CREATE_TABLE = "create table " + TABLE_NAME + "(" +
            ID + " integer primary key autoincrement," +
            COUNT + " integer" +
            ")";

    private RecordDao() {
        dbHelper = new DBHelper(App.getContext());
    }

    public static RecordDao getInstance() {
        return InnerDb.instance;
    }
    private static class InnerDb{
        private static RecordDao instance = new RecordDao();
    }


    /**
     * 数据库插入数据
     *
     */
    public synchronized <T> void insert(int count) {
        Log.e(TAG, "insert: " );
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        try {
            ContentValues cv = new ContentValues();
            cv.put(COUNT,count);
            db.insert(TABLE_NAME, null, cv);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.close();
        }
    }

    /**
     * 删除表中所有的数据
     */
    public synchronized void clearAll() {
        Log.e(TAG, "clearAll: " );
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String sql = "delete from " + TABLE_NAME;

        try {
            db.execSQL(sql);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.close();
        }
    }
    /**
     * 修改表中表中所有的数据
     */
    public synchronized void update(String id,int count) {
        Log.e(TAG, "update: "+id+"--"+count );
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues value = new ContentValues();
        value.put(COUNT,count);
        db.update(TABLE_NAME,value,ID+"=?",new String[]{id});

//        try {
//            db.execSQL(sql);
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            db.close();
//        }
    }

    /**
     * 查询数据
     *
     * @return List
     */
    @SuppressLint("Range")
    public synchronized <T> List<String> query() {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        List<String> list = new ArrayList<>();
        String querySql = "select * from " + TABLE_NAME;
        Cursor cursor = null;
        try {
            cursor = db.rawQuery(querySql, null);
            while (cursor.moveToNext()) {
                Log.e(TAG, "query: "+cursor.getString(cursor.getColumnIndex(ID))+"--"+cursor.getString(cursor.getColumnIndex(COUNT)));
                list.add( cursor.getString(cursor.getColumnIndex(COUNT)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (db != null) {
                db.close();
            }
        }
        return list;
    }
}
